<template>
  <div>
    <div class="margin-container-list" v-loading="loading">
      <el-form ref="form" :model="form" label-position="right" :rules="rules" label-width="120px">
        <el-form-item label="快递公司" prop="enterpriseExpressId" >
          <el-select v-model="form.enterpriseExpressId" placeholder="请选择" class="width-300">
            <el-option v-for="express in expressList" :key="express.enterpriseExpressId" :label="express.expressName" :value="express.enterpriseExpressId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="批次名称" prop="printBatchName" >
          <el-input v-model="form.printBatchName" placeholder="请输入批次名称" class="width-300"></el-input><br/>
        </el-form-item>
        <el-form-item label="">
          <el-checkbox v-model="isNotice">通知快递员现在上门揽件</el-checkbox>
        </el-form-item>
      </el-form>
    </div>
    <div class="dialog-footer mar-b-20">
      <el-button type="primary" @click="generateBatch">生成批次</el-button>
      <el-button plain @click="doCancel">取消</el-button>
    </div>
  </div>
</template>

<script>
  import { listExpress } from '@/api/goods/express'
  import { savePrintBatch } from '@/api/goods/orderPrintBatch'

  export default {
    components: {},
    props: {
      showFlag: {
        type: Boolean,
        default() {
          return false
        }
      },
      // 初始选中订单id
      initOrderIds: {
        type: Array,
        default() {
          return []
        }
      }
    },
    data() {
      return {
        form: {
          enterpriseId: this.getEnterpriseId(),
          enterpriseExpressId: '',
          printBatchName: '',
          noticeFlag: 1,
          orderIds: []
        },
        rules: {
          enterpriseExpressId: [
            { required: true, message: '请选择快递公司', trigger: 'blur' }
          ],
          printBatchName: [
            { required: true, message: '请输入批次名称', trigger: 'blur' }
          ]
        },
        expressList: [],
        loading: false,
        isNotice: false
      }
    },
    watch: {
      showFlag(val) {
        if (val) {
          this.resetSearch()
          this.form.orderIds = Object.assign([], this.initOrderIds)
          if (this.form.orderIds.length <= 0) this.doCancel()
        }
      }
    },
    // 初始化页面
    created() {
      this.form.orderIds = Object.assign([], this.initOrderIds)
      if (this.form.orderIds.length <= 0) this.doCancel()
      listExpress({ enterpriseId: this.getEnterpriseId(), supplierId: this.getSupplierId(), warehouseId: this.getWarehouseId(), expressStatus: 1 }).then(response => {
        if (response.isError) return
        this.expressList = response.data
      })
    },
    filters: {
    },
    mounted() {
    },
    methods: {
      // 生产批次事件
      generateBatch() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.form.noticeFlag = this.isNotice ? 0 : 1
            savePrintBatch(this.form).then(response => {
              this.loading = false
              if (response.isError) return
              this.$message({ type: 'success', message: '保存成功!' })
              this.$emit('close', true)
            })
          }
        })
      },
      // 重置事件
      resetSearch() {
        this.$refs['form'].resetFields()
        this.form = {
          enterpriseId: this.getEnterpriseId(),
          enterpriseExpressId: '',
          printBatchName: '',
          orderIds: []
        }
        this.isNotice = false
      },
      // 取消事件
      doCancel() {
        if (this.form.orderIds.length <= 0) {
          this.$message({ type: 'error', message: '无选择订单' })
        }
        this.$emit('close', false)
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
